Hanye官网
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <!-- 面包屑导航 -->
  13. <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
  14. <div class="max-w-screen-2xl mx-auto">
  15. <nuxt-link
  16. :to="`${homepagePath}/`"
  17. class="justify-start text-white/60 text-base font-normal"
  18. >{{ t("common.breadcrumb.home") }}</nuxt-link
  19. >
  20. <span class="text-white/60 text-base font-normal px-2"> / </span>
  21. <nuxt-link
  22. :to="`${homepagePath}/products`"
  23. class="text-white/60 text-base font-normal"
  24. >{{ t("common.breadcrumb.products") }}</nuxt-link
  25. >
  26. <span class="text-white/60 text-base font-normal px-2"> / </span>
  27. <nuxt-link
  28. v-if="product?.category"
  29. :to="`${homepagePath}/products?category=${encodeURIComponent(
  30. product.category
  31. )}`"
  32. class="text-white/60 text-base font-normal"
  33. >{{ product.category }}</nuxt-link
  34. >
  35. <span class="text-white/60 text-base font-normal px-2"> / </span>
  36. <span class="text-white text-base font-normal">{{
  37. product?.title || product?.name
  38. }}</span>
  39. </div>
  40. </div>
  41. <!-- 产品详情内容 -->
  42. <div
  43. v-if="product"
  44. class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
  45. >
  46. <div class="max-w-screen-2xl mx-auto">
  47. <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
  48. <!-- 左侧产品图片 -->
  49. <div
  50. class="flex flex-col gap-6 lg:sticky lg:top-24 self-start select-none"
  51. >
  52. <!-- 主图展示 -->
  53. <div
  54. class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
  55. >
  56. <!-- 主图容器 - Swiper -->
  57. <Swiper
  58. :modules="[Navigation, Pagination, EffectFade, Thumbs]"
  59. :slides-per-view="1"
  60. :pagination="{
  61. clickable: true,
  62. dynamicBullets: true,
  63. dynamicMainBullets: 3,
  64. }"
  65. :navigation="true"
  66. :effect="'fade'"
  67. :fade-effect="{ crossFade: true }"
  68. :thumbs="{ swiper: thumbsSwiper }"
  69. class="w-full h-full product-swiper"
  70. @swiper="swiperInstance = $event"
  71. @slideChange="currentSlideIndex = $event.activeIndex"
  72. >
  73. <SwiperSlide
  74. v-for="(image, slideIndex) in [
  75. product.image,
  76. ...(product.gallery || []),
  77. ]"
  78. :key="slideIndex"
  79. >
  80. <div class="relative w-full h-full">
  81. <!-- 当前图片 -->
  82. <img
  83. :src="image"
  84. :alt="`${product.name} - ${t('products.image', {
  85. index: slideIndex + 1,
  86. })}`"
  87. class="w-full h-full object-contain rounded-lg transition-all duration-500"
  88. :class="{
  89. 'opacity-0': isSlideThumbnailLoading[slideIndex],
  90. 'opacity-100':
  91. !isSlideThumbnailLoading[slideIndex] &&
  92. !slideThumbnailErrors[slideIndex],
  93. }"
  94. @load="handleSlideImageLoad(slideIndex)"
  95. @error="handleSlideImageError(slideIndex)"
  96. />
  97. <!-- 加载状态 -->
  98. <div
  99. v-if="isSlideThumbnailLoading[slideIndex]"
  100. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10"
  101. >
  102. <div
  103. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  104. ></div>
  105. </div>
  106. <!-- 错误提示 -->
  107. <div
  108. v-if="slideThumbnailErrors[slideIndex]"
  109. class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
  110. >
  111. <div class="flex flex-col items-center gap-2">
  112. <span class="text-white">{{
  113. t("products.imageLoadError")
  114. }}</span>
  115. <button
  116. @click.stop="retryLoadSlideImage(slideIndex)"
  117. class="px-4 py-2 bg-cyan-400 text-white rounded-lg hover:bg-cyan-600 transition-colors duration-300"
  118. >
  119. {{ t("products.retry") }}
  120. </button>
  121. </div>
  122. </div>
  123. </div>
  124. </SwiperSlide>
  125. </Swiper>
  126. <!-- 全局加载状态 - 仅在所有图片都未加载完成时显示 -->
  127. <div
  128. v-if="isInitialLoading"
  129. class="absolute inset-0 flex items-center justify-center bg-zinc-900/80 z-30"
  130. >
  131. <div
  132. class="animate-spin h-12 w-12 border-4 border-cyan-400 rounded-full border-t-transparent"
  133. ></div>
  134. </div>
  135. </div>
  136. <!-- 缩略图列表 - Swiper -->
  137. <div
  138. class="relative bg-zinc-900 rounded-lg py-6 px-8 overflow-hidden product-thumbnail-container"
  139. >
  140. <Swiper
  141. :modules="[Navigation, Thumbs, FreeMode]"
  142. :slides-per-view="'auto'"
  143. :space-between="12"
  144. :free-mode="true"
  145. :watch-slides-progress="true"
  146. :navigation="{
  147. nextEl: '.swiper-thumb-next',
  148. prevEl: '.swiper-thumb-prev',
  149. }"
  150. class="thumbs-swiper"
  151. @swiper="thumbsSwiper = $event"
  152. >
  153. <SwiperSlide
  154. v-for="(image, index) in [
  155. product.image,
  156. ...(product.gallery || []),
  157. ]"
  158. :key="index"
  159. class="!w-16 !h-16 md:!w-20 md:!h-20 !flex-shrink-0 cursor-pointer transition-all duration-300 relative"
  160. >
  161. <div
  162. class="w-full h-full rounded-lg overflow-hidden thumbnail-fixed-size"
  163. :class="{
  164. 'ring-2 ring-cyan-400 ring-offset-2 ring-offset-zinc-900':
  165. currentSlideIndex === index,
  166. 'hover:ring-1 hover:ring-cyan-400/50 hover:ring-offset-1 hover:ring-offset-zinc-900':
  167. currentSlideIndex !== index,
  168. 'opacity-50':
  169. isSlideThumbnailLoading[index] ||
  170. slideThumbnailErrors[index],
  171. }"
  172. >
  173. <!-- 缩略图加载状态 -->
  174. <div
  175. v-if="isSlideThumbnailLoading[index]"
  176. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg z-10"
  177. >
  178. <div
  179. class="animate-spin h-4 w-4 border-2 border-cyan-400 rounded-full border-t-transparent"
  180. ></div>
  181. </div>
  182. <!-- 缩略图遮罩 -->
  183. <div
  184. class="absolute inset-0 transition-all duration-300 rounded-lg"
  185. :class="{
  186. 'bg-black/30': currentSlideIndex === index,
  187. 'bg-black/0 hover:bg-black/20':
  188. currentSlideIndex !== index,
  189. }"
  190. ></div>
  191. <img
  192. :src="image"
  193. :alt="`${product.name} - ${t('products.image', {
  194. index: index + 1,
  195. })}`"
  196. class="w-full h-full object-cover transition-all duration-300 rounded-lg"
  197. :class="{
  198. 'opacity-0': isSlideThumbnailLoading[index],
  199. 'opacity-100':
  200. !isSlideThumbnailLoading[index] &&
  201. !slideThumbnailErrors[index],
  202. 'hover:scale-110': currentSlideIndex !== index,
  203. }"
  204. @load="handleSlideImageLoad(index)"
  205. @error="handleSlideImageError(index)"
  206. />
  207. <!-- 缩略图错误提示 -->
  208. <div
  209. v-if="slideThumbnailErrors[index]"
  210. class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
  211. >
  212. <div class="flex flex-col items-center gap-1">
  213. <span class="text-white text-xs">{{
  214. t("products.error")
  215. }}</span>
  216. <button
  217. @click.stop="retryLoadSlideImage(index)"
  218. class="px-2 py-1 bg-cyan-400 text-white text-xs rounded hover:bg-cyan-600 transition-colors duration-300"
  219. >
  220. {{ t("products.retry") }}
  221. </button>
  222. </div>
  223. </div>
  224. </div>
  225. </SwiperSlide>
  226. </Swiper>
  227. <!-- 缩略图导航按钮 -->
  228. <button
  229. class="swiper-thumb-prev absolute top-1/2 left-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  230. >
  231. <svg
  232. xmlns="http://www.w3.org/2000/svg"
  233. class="h-5 w-5 text-white"
  234. fill="none"
  235. viewBox="0 0 24 24"
  236. stroke="currentColor"
  237. >
  238. <path
  239. stroke-linecap="round"
  240. stroke-linejoin="round"
  241. stroke-width="2"
  242. d="M15 19l-7-7 7-7"
  243. />
  244. </svg>
  245. </button>
  246. <button
  247. class="swiper-thumb-next absolute top-1/2 right-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  248. >
  249. <svg
  250. xmlns="http://www.w3.org/2000/svg"
  251. class="h-5 w-5 text-white"
  252. fill="none"
  253. viewBox="0 0 24 24"
  254. stroke="currentColor"
  255. >
  256. <path
  257. stroke-linecap="round"
  258. stroke-linejoin="round"
  259. stroke-width="2"
  260. d="M9 5l7 7-7 7"
  261. />
  262. </svg>
  263. </button>
  264. </div>
  265. </div>
  266. <!-- 右侧产品信息 -->
  267. <div class="flex flex-col gap-8">
  268. <!-- 产品名称 -->
  269. <div class="bg-zinc-900 rounded-lg p-6">
  270. <h1 class="text-white text-3xl font-medium mb-4">
  271. {{ product.title || product.name }}
  272. </h1>
  273. <div class="text-[#71717A] text-lg leading-relaxed">
  274. {{ product.summary }}
  275. </div>
  276. </div>
  277. <!-- 产品参数 -->
  278. <div class="bg-zinc-900 rounded-lg p-6">
  279. <div class="grid grid-cols-1 gap-4">
  280. <div
  281. class="flex justify-between items-center py-2 border-b border-zinc-800"
  282. >
  283. <span class="text-[#71717A]">{{
  284. t("products.categoryTitle")
  285. }}</span>
  286. <span class="text-white font-medium"
  287. >{{ product.category
  288. }}<template v-if="product.tag"> / {{
  289. product.tag
  290. }}</template></span
  291. >
  292. </div>
  293. <div
  294. v-if="product.tag"
  295. class="flex justify-between items-center py-2 border-b border-zinc-800"
  296. >
  297. <span class="text-[#71717A]">{{
  298. t("products.seriesTitle")
  299. }}</span>
  300. <span class="text-white font-medium">{{
  301. product.series?.join(" / ")
  302. }}</span>
  303. </div>
  304. <div
  305. class="flex justify-between items-center py-2 border-b border-zinc-800"
  306. >
  307. <span class="text-[#71717A]">{{
  308. t("products.usageTitle")
  309. }}</span>
  310. <span class="text-white font-medium">{{
  311. product.usage?.join(", ")
  312. }}</span>
  313. </div>
  314. <div
  315. v-if="product.capacities && product.capacities.length > 0"
  316. class="flex justify-between items-center py-2"
  317. >
  318. <span class="text-[#71717A]">{{
  319. t("products.capacitiesTitle")
  320. }}</span>
  321. <span class="text-white font-medium">{{
  322. product.capacities?.join(" / ")
  323. }}</span>
  324. </div>
  325. </div>
  326. </div>
  327. <!-- 产品描述 -->
  328. <div
  329. v-if="product.description"
  330. class="bg-zinc-900 rounded-lg p-6"
  331. >
  332. <h2 class="text-white text-xl font-medium mb-6">
  333. {{ t("products.productDescription") }}
  334. </h2>
  335. <div
  336. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  337. >
  338. {{ product.description }}
  339. </div>
  340. </div>
  341. <div class="bg-zinc-900 rounded-lg p-6">
  342. <div
  343. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  344. >
  345. <ContentRenderer :value="product.content" />
  346. </div>
  347. </div>
  348. <!-- 相关产品 -->
  349. <div
  350. v-if="relatedProducts.length > 0"
  351. class="bg-zinc-900 rounded-lg p-6"
  352. >
  353. <h2 class="text-white text-xl font-medium mb-6">
  354. {{ t("products.relatedProducts") }}
  355. </h2>
  356. <div
  357. class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
  358. >
  359. <nuxt-link
  360. v-for="relatedProduct in relatedProducts"
  361. :key="relatedProduct.id"
  362. :to="`${homepagePath}/products/${relatedProduct.id}`"
  363. class="group"
  364. >
  365. <div
  366. class="bg-zinc-800 rounded-lg p-4 transition-all duration-300 hover:bg-zinc-700"
  367. >
  368. <div
  369. class="aspect-square mb-4 overflow-hidden rounded-lg"
  370. >
  371. <img
  372. :src="relatedProduct.image"
  373. :alt="relatedProduct.title || relatedProduct.name"
  374. class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
  375. />
  376. </div>
  377. <h3
  378. class="text-white text-lg font-medium mb-2 line-clamp-2"
  379. >
  380. {{ relatedProduct.title || relatedProduct.name }}
  381. </h3>
  382. <p class="text-[#71717A] text-sm line-clamp-2">
  383. {{ relatedProduct.summary }}
  384. </p>
  385. </div>
  386. </nuxt-link>
  387. </div>
  388. </div>
  389. </div>
  390. </div>
  391. </div>
  392. </div>
  393. </div>
  394. </ErrorBoundary>
  395. </div>
  396. </template>
  397. <script setup lang="ts">
  398. /**
  399. * 产品详情页面
  400. * 展示产品主图、参数和描述
  401. */
  402. import { useErrorHandler } from "~/composables/useErrorHandler";
  403. import { useRoute, useI18n, useAsyncData } from "#imports";
  404. import { queryCollection } from "#imports";
  405. import { ContentRenderer } from "#components";
  406. import { Swiper, SwiperSlide } from "swiper/vue";
  407. import {
  408. Navigation,
  409. Pagination,
  410. EffectFade,
  411. Thumbs,
  412. FreeMode,
  413. } from "swiper/modules";
  414. import type { Swiper as SwiperType } from "swiper";
  415. import "swiper/css";
  416. import "swiper/css/navigation";
  417. import "swiper/css/pagination";
  418. import "swiper/css/effect-fade";
  419. const { error, isLoading } = useErrorHandler();
  420. const route = useRoute();
  421. const { locale, t } = useI18n();
  422. const id = route.params.id as string;
  423. const swiperInstance = ref<SwiperType | null>(null);
  424. const thumbsSwiper = ref<SwiperType | null>(null);
  425. // 图片状态
  426. const currentSlideIndex = ref(0);
  427. const isInitialLoading = ref(true); // 初始加载状态
  428. const isSlideThumbnailLoading = ref<boolean[]>([]);
  429. const slideThumbnailErrors = ref<boolean[]>([]);
  430. // 滚动跟随相关
  431. const scrollContainer = ref<HTMLElement | null>(null);
  432. const isSticky = ref(false);
  433. const homepagePath = computed(() => {
  434. return locale.value === "zh" ? "" : `/${locale.value}`;
  435. });
  436. interface Product {
  437. id: string;
  438. name: string;
  439. usage: string[];
  440. capacities: string[];
  441. category: string;
  442. categoryId: string;
  443. description: string;
  444. summary: string;
  445. image: string;
  446. gallery: string[];
  447. body: string;
  448. content?: any;
  449. tag?: string;
  450. series?: string[];
  451. meta?: {
  452. series?: string[];
  453. name?: string;
  454. title?: string;
  455. image?: string;
  456. summary?: string;
  457. };
  458. title?: string;
  459. }
  460. /**
  461. * 使用queryCollection获取产品数据
  462. */
  463. const { data: productContent } = await useAsyncData(
  464. `product-${id}`,
  465. async () => {
  466. try {
  467. // 使用queryCollection从content目录获取数据
  468. const content = await queryCollection("content")
  469. .where("path", "LIKE", `/products/${locale.value}/${id}`)
  470. .first();
  471. return content;
  472. } catch (err) {
  473. console.error("Error fetching product content:", err);
  474. error.value = new Error(t("products.loadError"));
  475. return null;
  476. }
  477. }
  478. );
  479. /**
  480. * 获取分类信息
  481. */
  482. const { data: categoryContent } = await useAsyncData(
  483. `category-${productContent.value?.meta?.categoryId}`,
  484. async () => {
  485. if (!productContent.value?.meta?.categoryId) return null;
  486. try {
  487. const content = await queryCollection("content")
  488. .where(
  489. "path",
  490. "LIKE",
  491. `/categories/${locale.value}/${productContent.value.meta?.categoryId}`
  492. )
  493. .first();
  494. return content;
  495. } catch (err) {
  496. console.error("Error fetching category:", err);
  497. return null;
  498. }
  499. },
  500. {
  501. immediate: !!productContent.value?.meta?.categoryId,
  502. }
  503. );
  504. /**
  505. * 使用计算属性解析产品数据
  506. */
  507. const product = computed<Product | null>(() => {
  508. if (!productContent.value) return null;
  509. // 提取产品数据
  510. const meta = productContent.value.meta || {};
  511. return {
  512. id: id,
  513. name: String(meta.name || productContent.value.title || ""),
  514. title: String(productContent.value.title || meta.name || ""),
  515. usage: Array.isArray(meta.usage) ? meta.usage : [],
  516. capacities: Array.isArray(meta.capacities) ? meta.capacities : [],
  517. category: categoryContent.value?.title || "",
  518. categoryId: meta.categoryId || "",
  519. description: productContent.value.description || "",
  520. summary: String(meta.summary || ""),
  521. image: String(meta.image || ""),
  522. gallery: Array.isArray(meta.gallery) ? meta.gallery : [],
  523. body: productContent.value.body || "",
  524. content: productContent.value,
  525. tag: meta.tag || "",
  526. series: Array.isArray(meta.series) ? meta.series : [],
  527. meta: {
  528. series: Array.isArray(meta.series) ? meta.series : [],
  529. name: String(meta.name || ""),
  530. title: String(productContent.value.title || ""),
  531. image: String(meta.image || ""),
  532. summary: String(meta.summary || ""),
  533. },
  534. };
  535. });
  536. /**
  537. * 获取相关产品
  538. */
  539. const { data: relatedProductsContent } = await useAsyncData(
  540. `related-products-${id}`,
  541. async () => {
  542. try {
  543. // 获取产品列表
  544. const content = await queryCollection("content")
  545. .where("path", "LIKE", `/products/${locale.value}/%`)
  546. .all();
  547. return content;
  548. } catch (err) {
  549. console.error("Error fetching related products:", err);
  550. return [];
  551. }
  552. }
  553. );
  554. /**
  555. * 处理相关产品数据
  556. */
  557. const relatedProducts = computed(() => {
  558. if (!relatedProductsContent.value || !product.value) return [];
  559. // 获取当前产品的分类和系列
  560. const currentCategory = product.value.categoryId;
  561. const currentSeries = product.value.meta?.series || [];
  562. const currentProductId = id;
  563. return relatedProductsContent.value
  564. .filter((item: any) => {
  565. // 排除当前产品 - 多重检查确保排除
  566. if (item._path === `/products/${locale.value}/${id}`) return false;
  567. const meta = item.meta || {};
  568. if (meta.name === currentProductId) return false;
  569. const itemCategoryId = meta.categoryId || "";
  570. const itemSeries = Array.isArray(meta.series) ? meta.series : [];
  571. // 判断是否同类别或同系列
  572. const isSameCategory =
  573. currentCategory && itemCategoryId === currentCategory;
  574. const hasSameSeries =
  575. currentSeries.length > 0 &&
  576. itemSeries.some((series: string) => currentSeries.includes(series));
  577. // 返回同类别或同系列的产品
  578. return isSameCategory || hasSameSeries;
  579. })
  580. .map((item: any) => {
  581. const meta = item.meta || {};
  582. return {
  583. id: meta.name || "",
  584. name: meta.name || item.title || "",
  585. title: item.title || meta.name || "",
  586. image: meta.image || "",
  587. summary: meta.summary || "",
  588. category: meta.categoryId || "",
  589. series: Array.isArray(meta.series) ? meta.series : [],
  590. };
  591. })
  592. .slice(0, 6); // 最多显示6个相关产品
  593. });
  594. /**
  595. * 预加载下一张图片
  596. */
  597. function preloadNextImage(image: string) {
  598. currentSlideIndex.value =
  599. (currentSlideIndex.value + 1) % (product.value?.gallery?.length || 1);
  600. }
  601. /**
  602. * 处理图片加载完成
  603. */
  604. function handleSlideImageLoad(index: number) {
  605. isSlideThumbnailLoading.value[index] = false;
  606. slideThumbnailErrors.value[index] = false;
  607. // 检查是否所有图片都已加载
  608. const allImagesLoaded = isSlideThumbnailLoading.value.every(
  609. (status) => !status
  610. );
  611. if (allImagesLoaded) {
  612. isInitialLoading.value = false;
  613. }
  614. }
  615. /**
  616. * 处理图片加载错误
  617. */
  618. function handleSlideImageError(index: number) {
  619. isSlideThumbnailLoading.value[index] = false;
  620. slideThumbnailErrors.value[index] = true;
  621. }
  622. /**
  623. * 重试加载图片
  624. */
  625. function retryLoadSlideImage(index: number) {
  626. isSlideThumbnailLoading.value[index] = true;
  627. slideThumbnailErrors.value[index] = false;
  628. // 确定正确的图片URL
  629. const images = [product.value?.image, ...(product.value?.gallery || [])];
  630. const imageUrl = images[index];
  631. // 检查图片URL是否有效
  632. if (!imageUrl) {
  633. console.error("Invalid image URL:", index);
  634. slideThumbnailErrors.value[index] = true;
  635. isSlideThumbnailLoading.value[index] = false;
  636. return;
  637. }
  638. // 创建新的图片对象并设置超时
  639. const img = new Image();
  640. const timeoutId = setTimeout(() => {
  641. handleSlideImageError(index);
  642. }, 10000); // 10秒超时
  643. img.onload = () => {
  644. clearTimeout(timeoutId);
  645. handleSlideImageLoad(index);
  646. };
  647. img.onerror = (error) => {
  648. clearTimeout(timeoutId);
  649. console.error("Image load error:", { index, error });
  650. handleSlideImageError(index);
  651. };
  652. // 设置跨域属性
  653. img.crossOrigin = "anonymous";
  654. // 最后设置src以开始加载
  655. img.src = imageUrl;
  656. }
  657. // 页面加载时初始化状态
  658. onMounted(() => {
  659. // 初始化缩略图加载状态数组
  660. const galleryLength = (product.value?.gallery?.length || 0) + 1;
  661. isSlideThumbnailLoading.value = new Array(galleryLength).fill(true);
  662. slideThumbnailErrors.value = new Array(galleryLength).fill(false);
  663. // 设置初始加载状态
  664. isInitialLoading.value = true;
  665. // 预加载所有缩略图
  666. const images = [product.value?.image, ...(product.value?.gallery || [])];
  667. images.forEach((image, index) => {
  668. if (image) {
  669. const img = new Image();
  670. img.onload = () => handleSlideImageLoad(index);
  671. img.onerror = () => handleSlideImageError(index);
  672. img.src = image;
  673. }
  674. });
  675. // 添加滚动监听
  676. scrollContainer.value = document.querySelector(".max-w-screen-2xl");
  677. if (scrollContainer.value) {
  678. window.addEventListener("scroll", handleScroll, { passive: true });
  679. }
  680. });
  681. // 清理滚动监听
  682. onUnmounted(() => {
  683. if (scrollContainer.value) {
  684. window.removeEventListener("scroll", handleScroll);
  685. }
  686. });
  687. // 处理滚动事件
  688. function handleScroll() {
  689. if (!scrollContainer.value) return;
  690. const containerRect = scrollContainer.value.getBoundingClientRect();
  691. const scrollTop = window.scrollY || document.documentElement.scrollTop;
  692. // 当容器顶部距离视窗顶部小于100px时,启用sticky
  693. isSticky.value = containerRect.top < 100;
  694. }
  695. // SEO优化
  696. useHead(() => ({
  697. title: `${product.value?.name} - Hanye`,
  698. meta: [
  699. {
  700. name: "description",
  701. content: product.value?.description,
  702. },
  703. ],
  704. }));
  705. </script>
  706. <style lang="scss" scoped>
  707. /* 隐藏滚动条但保持滚动功能 */
  708. .scrollbar-hide {
  709. -ms-overflow-style: none; /* IE and Edge */
  710. scrollbar-width: none; /* Firefox */
  711. }
  712. .scrollbar-hide::-webkit-scrollbar {
  713. display: none; /* Chrome, Safari and Opera */
  714. }
  715. /* 图片过渡动画 */
  716. .main-image {
  717. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  718. }
  719. /* 缩略图悬停效果 */
  720. .thumbnail-item {
  721. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  722. }
  723. .thumbnail-item:hover {
  724. transform: translateY(-2px);
  725. }
  726. /* 缩略图选中效果 */
  727. .thumbnail-item.selected {
  728. transform: scale(1.05);
  729. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  730. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  731. }
  732. /* 产品信息卡片效果 */
  733. .info-card {
  734. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  735. }
  736. .info-card:hover {
  737. transform: translateY(-2px);
  738. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  739. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  740. }
  741. /* 滚动跟随效果 */
  742. .lg\:sticky {
  743. position: sticky;
  744. top: 6rem; /* 96px */
  745. transition: all 0.3s ease;
  746. z-index: 10;
  747. max-height: calc(100vh - 6rem);
  748. overflow-y: auto;
  749. }
  750. @media (max-width: 1024px) {
  751. .lg\:sticky {
  752. position: relative;
  753. top: 0;
  754. max-height: none;
  755. }
  756. }
  757. /* Swiper 样式定制 */
  758. .product-swiper {
  759. :deep(.swiper-pagination-bullet) {
  760. background-color: white;
  761. opacity: 0.5;
  762. }
  763. :deep(.swiper-pagination-bullet-active) {
  764. opacity: 1;
  765. background-color: theme("colors.cyan.400");
  766. }
  767. :deep(.swiper-button-next),
  768. :deep(.swiper-button-prev) {
  769. color: theme("colors.cyan.500");
  770. background-color: rgba(0, 0, 0, 0.3);
  771. width: 36px;
  772. height: 36px;
  773. border-radius: 50%;
  774. &:after {
  775. font-size: 16px;
  776. font-weight: bold;
  777. }
  778. &:hover {
  779. background-color: theme("colors.cyan.500");
  780. color: white;
  781. }
  782. }
  783. :deep(.swiper-button-disabled) {
  784. opacity: 0.35;
  785. cursor: auto;
  786. pointer-events: none;
  787. }
  788. :deep(.swiper-slide) {
  789. display: flex;
  790. align-items: center;
  791. justify-content: center;
  792. }
  793. }
  794. /* 缩略图Swiper样式 */
  795. .thumbs-swiper {
  796. overflow: visible;
  797. padding: 0.25rem;
  798. min-height: 6rem;
  799. :deep(.swiper-wrapper) {
  800. align-items: center;
  801. display: flex;
  802. min-height: 6rem;
  803. }
  804. :deep(.swiper-slide) {
  805. width: auto;
  806. height: auto;
  807. opacity: 0.7;
  808. transition: all 0.3s ease;
  809. display: flex;
  810. align-items: center;
  811. justify-content: center;
  812. }
  813. :deep(.swiper-slide-thumb-active) {
  814. opacity: 1;
  815. transform: scale(1.05);
  816. z-index: 1;
  817. }
  818. }
  819. /* 自定义缩略图导航按钮 */
  820. .swiper-thumb-next,
  821. .swiper-thumb-prev {
  822. &:focus {
  823. outline: none;
  824. }
  825. &.swiper-button-disabled {
  826. opacity: 0.3;
  827. cursor: default;
  828. background-color: rgba(0, 0, 0, 0.2);
  829. &:hover {
  830. background-color: rgba(0, 0, 0, 0.2);
  831. }
  832. }
  833. }
  834. /* 响应式调整 */
  835. @media (max-width: 768px) {
  836. .thumbs-swiper {
  837. padding: 0;
  838. :deep(.swiper-slide) {
  839. margin-right: 8px;
  840. }
  841. }
  842. /* 确保缩略图容器在移动设备上有足够空间 */
  843. .product-thumbnail-container {
  844. padding: 1rem;
  845. .swiper-thumb-prev {
  846. left: 0.5rem;
  847. width: 2rem;
  848. height: 2rem;
  849. }
  850. .swiper-thumb-next {
  851. right: 0.5rem;
  852. width: 2rem;
  853. height: 2rem;
  854. }
  855. }
  856. }
  857. /* 解决不同分辨率下缩略图大小问题 */
  858. .thumbnail-fixed-size {
  859. width: 100%;
  860. height: 100%;
  861. aspect-ratio: 1 / 1;
  862. position: relative;
  863. }
  864. </style>